package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.QuestionBankCategory;
import com.jyy.prefabricated.entity.SysUser;
import com.jyy.prefabricated.service.QuestionBankCategoryService;
import com.jyy.prefabricated.utils.UserSessionUtil;
import com.jyy.prefabricated.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 题库分类
 *
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class QuestionBankCategoryController {

    @Autowired
    private QuestionBankCategoryService questionBankCategoryService;

    @Resource
    private UserUtils userUtils;

    /**
     * 新增分类
     *
     * @param questionBankCategory
     * @return
     */
    @PostMapping("/question-bank-category")
    public AjaxResult insert(@Valid @RequestBody QuestionBankCategory questionBankCategory) {
        log.info("Rest request to insert questionBankCategory:{}", questionBankCategory);
        int id = questionBankCategoryService.insert(questionBankCategory);
        QuestionBankCategory result = questionBankCategoryService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新分类
     *
     * @param questionBankCategory
     * @return
     */
    @PutMapping("/question-bank-category")
    public AjaxResult update(@Valid @RequestBody QuestionBankCategory questionBankCategory) {
        log.info("Rest request to update questionBankCategory:{}", questionBankCategory);
        questionBankCategoryService.update(questionBankCategory);
        QuestionBankCategory result = questionBankCategoryService.selectById(questionBankCategory.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询分类详情
     *
     * @param id
     * @return
     */
    @GetMapping("/question-bank-category/{id}")
    public AjaxResult selectById(@PathVariable Integer id) {
        log.info("Rest request to find questionBankCategory by id:{}", id);
        QuestionBankCategory result = questionBankCategoryService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping("/question-bank-category/{id}")
    public AjaxResult deleteById(@PathVariable Integer id) {
        log.info("Rest request to delete questionBankCategory by id:{}", id);
        int row = questionBankCategoryService.delete(id);
        if (row > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 获取分类列表
     *
     * @param category 名称
     * @param pid      父级id
     * @return
     */
    @GetMapping("/question-bank-category-list")
    public AjaxResult selectAll(@RequestParam(required = false, defaultValue = "") String category, @RequestParam(required = false) Integer pid) {
        log.info("Rest request to find questionBankCategory by category:{},pid:{}", category, pid);
        SysUser user = null;
        Integer trainingOrganizationId = null;
        String token = UserSessionUtil.resolveToken();
        if (StringUtils.isNotBlank(token)) {
            user = userUtils.getCurrentUser();
        }
        if (user != null) {
            if (user.getTrainingOrganization() != null) {
                trainingOrganizationId = user.getTrainingOrganization().getId();
            }
        }
        List<QuestionBankCategory> result = questionBankCategoryService.selectByParams(category, pid, trainingOrganizationId);
        if (result != null && result.size() > 0) {
            for (QuestionBankCategory questionBankCategory : result) {
                questionBankCategory.setCategoryId(questionBankCategory.getId());
            }
        }

        return AjaxResult.success(result);
    }

    /**
     * 分页获取分类列表
     *
     * @param category 名称
     * @param pid      父级id
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/question-bank-category")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String category, @RequestParam(required = false) Integer pid, Pageable pageable) {
        log.info("Rest request to find questionBankCategory by category:{},pid:{}", category, pid);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        SysUser user = null;
        Integer trainingOrganizationId = null;
        String token = UserSessionUtil.resolveToken();
        if (StringUtils.isNotBlank(token)) {
            user = userUtils.getCurrentUser();
        }
        if (user != null) {
            if (user.getTrainingOrganization() != null) {
                trainingOrganizationId = user.getTrainingOrganization().getId();
            }
        }
        List<QuestionBankCategory> categories = questionBankCategoryService.selectByParams(category, pid, trainingOrganizationId);
        PageInfo<QuestionBankCategory> result = new PageInfo<>(categories);
        return AjaxResult.success(result);
    }

}
